package com.mida.app.report.controller.config;


import com.mida.tool.common.cons.RespConst;
import com.mida.tool.common.exception.MassertException;
import com.mida.tool.common.result.ActionResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理所有的自定义断言异常
     * @param req
     * @param e
     * @return
     */
//    @ExceptionHandler(value = MassertException.class)
//    public RespBody exceptionHandler(HttpServletRequest req, MassertException e){
//        return new RespBody(e.getRetMsg(), e.getOtherMsg());
//    }

    @ExceptionHandler(value = MassertException.class)
    public ActionResult exceptionHandler(HttpServletRequest req, MassertException e) {
        return new ActionResult(HttpStatus.BAD_REQUEST.value(),e.getRetMsg().getCode(),e.getRetMsg().getMsg());
    }


    /**
     * 处理其它异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public ActionResult exceptionHandler(HttpServletRequest req, Exception e){
        log.error("未捕获的异常:",e);
        return new ActionResult(HttpStatus.INTERNAL_SERVER_ERROR.value(),RespConst.SysErr.SYS_COMM_ERR.getCode(),
                RespConst.SysErr.SYS_COMM_ERR.getMsg());
    }
}
